package com.unitd.base.module.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.unitd.base.frame.plugin.module.dto.ResponseUtils;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.annotation.LoginUser;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.pojo.dto.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@Api(tags = "公司用户信息")
@RestController
@RequestMapping("/api/corpuser")
public class ApiCorpUserController extends ApiBaseAction {
//    @Autowired
//    private ApiCorpUserService CorpUserService;
    
    //测试代码
    public static List<UserVO> CorpUserEntities = new ArrayList<UserVO>();
//    static{
//        UserVo corpuser1 = new UserVo();
//        corpuser1.setUserId(new Long(10001));
//        corpuser1.setUsername("张三");
//        corpuser1.setGender(new Integer(1));
//        corpuser1.setUser_level_id(new Integer(1));
//        corpuser1.setMobile("13611111111");
//        UserVo corpuser2 = new UserVo();
//        corpuser2.setUserId(new Long(10002));
//        corpuser2.setUsername("李四");
//        corpuser2.setGender(new Integer(1));
//        corpuser2.setUser_level_id(new Integer(1));
//        corpuser2.setMobile("13622222222");
//        CorpUserEntities.add(corpuser1);
//        CorpUserEntities.add(corpuser2);
//    }

    /**
     * 获取用户的公司用户信息
     */
    @ApiOperation(value = "获取用户的公司用户信息接口", response = Map.class)
    @PostMapping("list")
    @IgnoreAuth
    public Object list(@LoginUser UserVO loginUser) {
        //测试代码
        //Map<String, Object> param = Maps.newHashMap();
        //param.put("user_id", loginUser.getUserId());
//        List<UserVo> CorpUserEntities = CorpUserService.queryList(param);//测试代码
       return ResponseUtils.toResponsSuccess(CorpUserEntities);
    }

    /**
     * 获取公司用户信息的详情
     */
    @ApiOperation(value = "获取公司用户信息的详情", response = Map.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "公司用户ID", required = true, dataType = "Integer")})
    @PostMapping("detail")
    @IgnoreAuth
    public Object detail(Integer id, @LoginUser UserVO loginUser) {
        //测试代码
//        UserVo entity = CorpUserService.queryObject(id);
         UserVO entity = new UserVO();
         for(UserVO corpuser:CorpUserEntities){
             if(String.valueOf(corpuser.getUserId()).equals(String.valueOf(id))){
                 entity = corpuser;
                 entity.setUserId(corpuser.getUserId());
                 entity.setGender(corpuser.getGender());
//                 entity.setUser_level_id(corpuser.getUser_level_id());
                 entity.setMobile(corpuser.getMobile());
             }
         }
        //判断越权行为
//        if (!entity.getUserId().equals(loginUser.getUserId())) {
//            return ResponseUtils.toResponsObject(403, "您无权查看", "");
//        }
       return ResponseUtils.toResponsSuccess(entity);
    }

    /**
     * 添加或更新公司用户信息
     */
    @ApiOperation(value = "添加或更新公司用户信息", response = Map.class)
    @PostMapping("save")
    @IgnoreAuth
    public Object save(@LoginUser UserVO loginUser) {
        JSONObject CorpUserJson = this.getJsonRequest();
        UserVO entity = new UserVO();
//        if (null != CorpUserJson) {
//            entity.setUserId(CorpUserJson.getLong("userId"));
//            entity.setUsername(CorpUserJson.getString("username"));
//            entity.setGender(CorpUserJson.getInteger("gender"));
//            entity.setUser_level_id(CorpUserJson.getInteger("user_level_id"));
//            entity.setMobile(CorpUserJson.getString("mobile"));
//        }
        //测试代码
        for(UserVO corpuser:CorpUserEntities){
            if(String.valueOf(corpuser.getUserId()).equals(String.valueOf(entity.getUserId()))){
                CorpUserEntities.remove(corpuser);
            }
        }
        CorpUserEntities.add(entity)  ;
//        if (null == entity.getId() || entity.getId() == 0) {
//            entity.setId(null);
//            CorpUserService.save(entity);
//        } else {
//            CorpUserService.update(entity);
//        }
       return ResponseUtils.toResponsSuccess(entity);
    }

    /**
     * 删除指定的公司用户信息
     */
    @ApiOperation(value = "删除指定的公司用户信息", response = Map.class)
    @PostMapping("delete")
    @IgnoreAuth
    public Object delete(@LoginUser UserVO loginUser) {
        JSONObject jsonParam = this.getJsonRequest();
        Long id = jsonParam.getLongValue("id");
        //测试代码
        for(UserVO corpuser:CorpUserEntities){
            if(String.valueOf(corpuser.getUserId()).equals(String.valueOf(id))){
                CorpUserEntities.remove(corpuser);
            }
        }
        //UserVo entity = CorpUserService.queryObject(id);
        //判断越权行为
//        if (!entity.getUserId().equals(loginUser.getUserId())) {
//            return ResponseUtils.toResponsObject(403, "您无权删除", "");
//        }
//        CorpUserService.delete(id);
       return ResponseUtils.toResponsSuccess("");
    }
}
